<template>
    <div class="">
        <el-dialog :title="title" :visible.sync="dialog" width="40%" :before-close="dialogClose">
            <el-form ref="formData" :model="formData" label-width="100px">
				<el-form-item label="所属村级" prop="hamletid">
				    <el-select v-model="formData.hamletid" style="width: 100%;" placeholder="请选择所属村级" @change="changeHamlet">
				    	<el-option :label="item.title" :value="item.id" v-for="(item, index) in Hamlet"></el-option>
				    </el-select>
				</el-form-item>
				<el-form-item label="个人信息" prop="userid">
				    <el-select filterable v-model="formData.userid" style="width: 100%;" placeholder="请选择个人信息" @change="changeUser">
				    	<el-option :label="item.name" :value="item.id" v-for="(item, index) in User"></el-option>
				    </el-select>
				</el-form-item>
				<el-row>
					<el-col :span="12">
						<el-form-item label="姓名" prop="name">
						    <el-input v-model="formData.name" disabled placeholder="请输入姓名"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="手机号" prop="phone">
						    <el-input v-model="formData.phone" placeholder="请输入手机号"></el-input>
						</el-form-item>
					</el-col>
				</el-row>

				<el-row>
					<el-col :span="12">
						<el-form-item label="身份证号" prop="idcard">
						    <el-input v-model="formData.idcard" disabled placeholder="请输入身份证号"></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="性别" prop="sex">
						    <el-select v-model="formData.sex" style="width: 100%;" placeholder="请选择性别">
						    	<el-option :label="item" :value="item" v-for="(item, index) in Site.sex"></el-option>
						    </el-select>
						</el-form-item>
					</el-col>
				</el-row>
				
				<el-row>
					<el-col :span="12">
						<el-form-item :label="unitTxt" prop="unit">
						    <el-input v-model="formData.unit" placeholder="请输入工作单位"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="职务" prop="job">
						    <el-input v-model="formData.job" placeholder="请输入职务"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12" v-if="this.formData.type != 2">
						<el-form-item label="在家亲属" prop="unit">
						    <el-input v-model="formData.relatives" placeholder="请输入在家亲属"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="亲属电话" prop="contact">
							<el-input v-model="formData.contact" placeholder="请输入亲属电话"></el-input>
						</el-form-item>
					</el-col>
				</el-row>				


            	<el-form-item>
            		<el-button @click="dialogClose">取 消</el-button>
            		<el-button type="primary" @click="formSubmit">确 定</el-button>
            	</el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
			return {
				dialog: false,
				Site: [],
				User: [],
				Hamlet: [],
				unitTxt: '工作单位',
				title: '编辑从政',
				formData: {
					id: 0,
					userid: '', //个人id
					hamletid: '', //所属村级
					name: '', //姓名
					idcard: '', //身份证
					phone: '', //手机号
					sex: '', //性别
					unit: '', //工作单位
					job: '', //职务
					contact: '', //亲属电话
					type: 1, //类型 1:从政;2:从商;3:从军
				}
			}
        },
		created() {
		},
		watch: {
			dialog () {
				if(!this.dialog) {
					this.clearFormData()
					this.$emit('getJob')
				} else {
					if(this.formData.type == 1) {
						this.title = '编辑从政'
						this.unitTxt = '工作单位'
					}
					if(this.formData.type == 2) {
						this.title = '编辑从商'
						this.unitTxt = '公司名称'
					}
					if(this.formData.type == 3) {
						this.title = '编辑从军'
						this.unitTxt = '部队名称'
					}
					this.getUser()
					this.getSite()
					this.getHamlet()
				}
			}
		},
        methods: {
			getHamlet () {
				this.$apiPost('Hamlet/getHamlet').then(res => {
					if(res.code == 1) {
						this.Hamlet = res.data
					}
				})
			},
			getSite () {
				this.$apiPost('Index/getSiteFile').then(res => {
					if(res.code == 1) {
						this.Site = res.data
					}
				})
			},
			changeUser(e){
				console.log(e)
				var user = this.User.find(item=>item.id == e)
				this.formData.name = user.name
				this.formData.idcard = user.idcard
				this.formData.phone = user.phone
			},
			changeHamlet(e){
				this.getUser()
			},
			getUser () {
				this.$apiPost('User/getUser',{hamletid:this.formData.hamletid}).then(res => {
					if(res.code == 1) {
						this.User = res.data
					}
				})
			},
			dialogClose(){
				this.$emit('dialogToggle')
			},
			clearFormData () {
				for(let i in this.formData){
					if(i == 'id') {
						this.formData[i] = 0
					} else {
						this.formData[i] = ''
					}
				}
			},
			// 保存编辑
			formSubmit() {
				this.$apiPost('Job/editData', this.formData).then(res => {
					if(res.code == 1) {
						this.$message(res.msg)
						this.dialogClose()
					} else {
						this.$message(res.msg)
					}
				})
			},
        }
    };
</script>
<style scoped>
	.mselect{
		width: 100%;
	}
	.mtdiv{
		margin-top: 10px;
		display: inline-block;
		width: 100%;
	}
	.mtdiv .mtdiv-v{
		float: left;
		width: 50%;
		margin-bottom: 5px;
		display: flex;
		align-items: center;
	}
	.mtdiv .mtdiv-v label{
		float: left;
		margin-right: 10px;
	}
	.mtdiv .el-input{
		float: left;
		width: 80%;
		margin: 5px 0;
	}
	.el-cascader {
		line-height: 32px !important;
	}
	.el-input__inner {
	    height: 32px !important;
	    line-height: 32px !important;
	}
	.el-input__icon {
		line-height: 32px !important;
	}
</style>
